package rz.regex;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	/**
	 * @author dingdahai Mar 25, 2010 2:43:41 PM
	 * @param args
	 */
	public static void main(String[] args) {
		//drawStr();
		//replaceSpace();//
		//group();
		
		 
		 //System.out.println("aa|bb|cc|".split("[|]").length);
		
		quote();
	}

	public static void drawStr() {
		String regEx = ".+\\\\(.+)$";
		regEx = "\\d+\\.";
		String str = "c:\\dir1\\dir2\\name.txt";
		str = "1.4845.4857.";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		boolean rs = m.find();
		//for (int i = 0; i <= m.groupCount(); i++) {
			System.out.println(m.group(0));
		//}
		//String s=m.replaceAll("A"); 
	}
	
	public static void match() {
		String regEx = "^(0(\\d{10,11}))|(1(3|5|8|9)\\d{9})$";
		Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher("057198745628");
		if (matcher.matches()) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
	}
	
	//替换掉html标签中的空格
	public static void replaceSpace() {
		String html = "<table&nbsp;style=\"WIDTH:&nbsp;108pt;&nbsp;BORDER-COLLAPSE:&nbsp;collapse\"&nbsp;cellspacing=\"0\"&nbsp;cellpadding=\"0\"&nbsp;width=\"144\"&nbsp;border=\"0\"&nbsp;x:str=\"\"><br>&nbsp;&nbsp;&nbsp;&nbsp;<colgroup><col&nbsp;style=\"WIDTH:&nbsp;54pt\"&nbsp;span=\"2\"&nbsp;width=\"72\"></col></colgroup><br>&nbsp;&nbsp;&nbsp;&nbsp;<tbody><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr&nbsp;style=\"HEIGHT:&nbsp;33.75pt\"&nbsp;height=\"45\"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td&nbsp;class=\"xl65\"&nbsp;style=\"WIDTH:&nbsp;54pt;&nbsp;HEIGHT:&nbsp;33.75pt\"&nbsp;width=\"72\"&nbsp;height=\"45\">未实现</td><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td&nbsp;class=\"xl66\"&nbsp;style=\"BORDER-LEFT:&nbsp;medium&nbsp;none;&nbsp;WIDTH:&nbsp;54pt\"&nbsp;width=\"72\"&nbsp;x:num=\"40651\">2011-4-18</td><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tr><br>&nbsp;&nbsp;&nbsp;&nbsp;</tbody><br></table>&nbsp</td></tr><tr><td class=\"only_td\" align=\"center\" nowrap=\"true\" width=\"20%\">附件</td><td colspan=\"3\" nowrap=\"true\" width=\"30%\">&nbsp;</td></tr>";
		String tagRegex = "<[^>]*>";
		Pattern p = Pattern.compile(tagRegex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(html);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, m.group().replace("&nbsp;", "      "));
		}
		m.appendTail(sb);
		System.out.println(html);
		System.out.println(sb);
	}
	
	public static void group() {
		String regex = "(\\S(\\d)\\S)+";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher("a2b1c3ef4gf6");
		while (m.find()) {
			try {
				System.out.println(m.group() + ", " + m.group(1) + ", " + m.group(2));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 转义字符,正则, "."表示任意字符
	 * \Q Nothing, but quotes all characters until \E
		\E Nothing, but ends quoting started by \Q
	 */
	public static void quote() {
		http: //www.baidu.com
		System.out.println("\\" + ", " + Matcher.quoteReplacement("\\") + ", " + Pattern.quote(".") );
		System.out.println(Test.class.getName().replaceAll(Pattern.quote("."), "/"));//quote后 \Q.\E
	}
}
